package cn.edu.nks.dormitory.student.editinfo.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import cn.edu.nks.dormitory.model.StudentInfo;
import cn.edu.nks.dormitory.student.editinfo.db.IDBService;
import cn.edu.nks.dormitory.student.editinfo.dbimpl.DBService;
/**
 * 学生编辑个人信息
 * @author wangbs
 *
 */
public class EditStudentInfoAction extends HttpServlet{
	
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(EditStudentInfoAction.class);
	private static IDBService idbService = new DBService();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("application/jsons;charset=utf-8");
		String stuInfo = req.getParameter("stuInfo");
		logger.warn(stuInfo);
		JSONObject obj = JSONObject.fromObject(stuInfo);
		StudentInfo info = (StudentInfo) JSONObject.toBean(obj, StudentInfo.class);
		logger.warn(String.format("update student info jsons :%s", info.toString()));
		boolean flag = idbService.updateStudentInfo(info);
		
		JSONObject jsonObject = new JSONObject();
		if(flag == true){
			jsonObject.put("status", "succ");
		}else{
			jsonObject.put("status", "fail");
		}
		PrintWriter out = resp.getWriter();
		out.write(jsonObject.toString());
	}
}
